<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use EasyWeChat;

class SendWechatTemplate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $params = [];
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($params)
    {
        $this->params = $params;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $template_id = $this->params['template_id'];
        $user_id = $this->params['user_id'];

        $user = \DB::table('users')->select(['id','unionid'])->where('id',$user_id)->first();
        $officialAccount = EasyWeChat::officialAccount(); // 公众号
        $wechat_user = \DB::table('wechat_users')->where('unionid',$user->unionid)->first();

        if(!empty($wechat_user))
        {
            switch ($template_id)
            {
                //type=1 卖书订单提交, type=2 卖书订单审核打款,
                case 'fDri6AUO73KHgibmDn8v8vm1esCaNMCML_eesYlb6Ns':
                    $openid = $wechat_user->openid;
                    $type = isset($this->params['type']) ? $this->params['type'] : 1;

                    if($type == 1)
                    {
                        //卖书订单提交
                        $order = \DB::table('sell_orders')->select(['id','no'])->where('id',$this->params['sell_order_id'])->first();

                        $first = "卖书订单已提交成功，等待审核\r";
                        $officialAccount->template_message->send([
                            'touser'        => $openid,
                            'template_id'   => $template_id,
                            'url'  => '',
                            'miniprogram' => [
                                'appid'     => config('wechat.mini_program.default.app_id'),
                                'path'  => '/pages/sale/order/order-detail/order-detail?id='.$order->id,
                            ],
                            'data' => [
                                'first'         => $first,
                                'OrderSn'       => $order->no,
                                'OrderStatus'   => '订单提交成功'."\r\n",
                                'remark'        => '我们会尽快对订单进行审核，审核通过后将为您安排快递上门取书，请留意通知。'
                            ],
                        ]);
                    }
                    elseif($type == 2)
                    {
                        //卖书订单审核打款
                        $order = \DB::table('sell_orders')->select(['id','no'])->where('id',$this->params['sell_order_id'])->first();
                        $openid = $wechat_user->openid;
                        $number1 = $this->params['number1'];
                        $number2 = $this->params['number2'];
                        $actual_price = $this->params['actual_price'];

                        $first = "卖书订单已验收完成\r\n"."您卖的书其中{$number1}本验收通过，{$number2}本验收不通过，总收入{$actual_price}元。\r\n";
                        $officialAccount->template_message->send([
                            'touser'        => $openid,
                            'template_id'   => $template_id,
                            'url'  => '',
                            'miniprogram' => [
                                'appid'     => config('wechat.mini_program.default.app_id'),
                                'path'  => '/pages/sale/order/order-detail/order-detail?id='.$order->id,
                            ],
                            'data' => [
                                'first'         => $first,
                                'OrderSn'       => $order->no,
                                'OrderStatus'   => '验收完成，已打款'."\r\n",
                                'remark'        => '在个人主页「我的余额」可进行书费提现。如需取回被拒收的书，请在72小时内在卖书订单详情选择拒书取回。点击本消息可直接进入。'
                            ],
                        ]);
                    }
                    break;
                //我邀请的好友首次下单买书，订单状态为「已发货」时，推送此条消息
                case 'IZIf7iWG7W_yZuapB6bSfo4do97wzB_JIEH6O9CyAiQ':
                    $type = $this->params['type'];  //1成功下单买书 2成功下单卖书
                    $openid = $wechat_user->openid;

                    $coupon = \DB::table('coupons')->where('type',$this->params['coupon_type'])->first();

                    $wechat_nickname = $this->params['wechat_nickname'];
                    if($type == 1)
                    {
                        $first = "获得红包提醒\r\n"."你邀请的好友{$wechat_nickname}成功下单买书，你获得一个".$coupon->amount."元买书抵扣红包，订单满".$coupon->order_price."元可用，有效期".$coupon->days."天。\r\n";
                    }
                    else
                    {
                        $first = "获得红包提醒\r\n"."你邀请的好友{$wechat_nickname}成功下单卖书，你获得一个".$coupon->amount."元买书抵扣红包，订单满".$coupon->order_price."元可用，有效期".$coupon->days."天。\r\n";
                    }

                    $officialAccount->template_message->send([
                        'touser'        => $openid,
                        'template_id'   => $template_id,
                        'url'  => '',
                        'miniprogram' => [
                            'appid'     => config('wechat.mini_program.default.app_id'),
                            'path'  => '/pages/red-package/red-package'
                        ],
                        'data' => [
                            'first'      => $first,
                            'keyword1'   => $coupon->amount,
                            'keyword2'   => date('Y-m-d H:i:s'),
                            'keyword3'   => $coupon->amount."元买书抵扣红包\r\n",
                            'remark'     => '点击查看红包详情'
                        ],
                    ]);
                    break;
                case 'a2hKb0lOF-B9zAFMd9bR6aW0Zj0nS6UaXsWBGX8jqO4':
                    //书评审核通过
                    $openid = $wechat_user->openid;
                    $book = \DB::table('books')->select(['id','title'])->where('id',$this->params['book_id'])->first();

                    $first = "您提交的《".$book->title."》这本书的书评，已经通过审核。我们会将您的书评优先展示在这本书的详情页。\r\n";
                    $officialAccount->template_message->send([
                        'touser'        => $openid,
                        'template_id'   => $template_id,
                        'url'  => '',
                        'miniprogram' => [
                            'appid'     => config('wechat.mini_program.default.app_id'),
                            'path'  => '/pages/pingjia/pingjia'
                        ],
                        'data' => [
                            'first'      => $first,
                            'keyword1'   => '书评通过审核',
                            'keyword2'   => date('Y-m-d H:i:s')."\r\n",
                            'remark'     => '感谢您帮助了其他书友能够更快的了解这本书'
                        ],
                    ]);
                    break;
                case 'WVlGoIcGZPWVbHoSn6eeZE62G_cuZRgKtO9K35JhufM':
                    $first = "您已成功提现一笔金额到您的微信钱包-零钱账户，请查看。\r\n";
                    $openid = $wechat_user->openid;
                    $officialAccount->template_message->send([
                        'touser'        => $openid,
                        'template_id'   => $template_id,
                        'url'  => '',
                        'miniprogram' => [
                            'appid'     => config('wechat.mini_program.default.app_id'),
                            'path'  => '/pages/account/account'
                        ],
                        'data' => [
                            'first'      => $first,
                            'keyword1'   => $this->params['keyword1'],
                            'keyword2'   => $this->params['keyword2'],
                            'keyword3'   => $this->params['keyword3']."\r\n",
                            'remark'     => '提现到账的时间请以微信通知时间为准，正常情况下为立即到账。'
                        ],
                    ]);
                    break;
            }
        }

    }
}
